/**
 * 
 */
package com.feib.stms.action;

import java.util.Date;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.EmptyResultDataAccessException;

import com.feib.stms.model.PreDesignatedAccount;
import com.feib.stms.model.Securities;
import com.feib.stms.model.User;
import com.feib.stms.service.PreDesignatedAccountService;
import com.feib.stms.service.SecuritiesService;
import com.feib.stms.util.UserUtil;

/**
 * @title (#)Stms9011ActionBean.java<br>
 * @description 券商約定帳號維護 <br>
 * @author Karl Chen<br>
 * @version 1.0.0 2012/07/01
 * @2012/07/01 create by Karl Chen<br>
 * 
 */
public class Stms9011ActionBean extends BaseActionBean {
	
	private static final long serialVersionUID = -3945238981921190426L;

	private static final String DEFAULT_JSP = "/WEB-INF/views/stms9011/stms9011.jsp";

	/**
	 * 券商代號
	 */
	private String securitiesNo;
	
	/**
	 * 券商名稱
	 */
	private String securitiesName;
	
	/**
	 * 是否啟用
	 */
	private String is_Enabled_Type;
	
	/**
	 * 約定帳戶代號1  H.一般上市櫃,
	 */
	private String account_No_H1;
	
	/**
	 * 約定帳戶代號2  H.一般上市櫃,
	 */
	private String account_No_H2;
	
	/**
	 * 約定帳戶代號1  R.興櫃,
	 */
	private String account_No_R1;
	
	/**
	 * 約定帳戶代號2  R.興櫃,
	 */
	private String account_No_R2;
	
	/**
	 * 約定帳戶代號1  P.調撥;
	 */
	private String account_No_P1;
	
	/**
	 * 約定帳戶代號2  P.調撥;
	 */
	private String account_No_P2;	
	
	/**
	 * 是否新增約定帳號維護
	 * 0表示更新,1表示新增
	 */
	private String isNewPreDesignatedAccount = "0";
	
	/**
	 * 操作群組Pk
	 */
	private Long groupPk;
	
	/**
	 * 分公司Pk
	 */
	private Long securitiesPk;
	
	/**
	 * 券商約定帳號維護PK
	 */
	private Long preDesignatedAccountPk;
	
	/**
	 * 約定帳號對象
	 */
	private PreDesignatedAccount preDesignatedAccount;
	
    @SpringBean("securitiesService")
    private SecuritiesService securitiesService;
	
	@SpringBean("preDesignatedAccountService")
    private PreDesignatedAccountService preDesignatedAccountService;
	
	private String message;
	
	protected String errorMessage;
	
	
	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	/**
	 * @return message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message 的設定的 message
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	@DontBind
	@DefaultHandler
	public Resolution doDefault() {
		logger.debug("Enter doDefault method.");
		return new ForwardResolution(DEFAULT_JSP);
	}

	/**
	 * 查詢
	 * 
	 * @return
	 */
	public Resolution doQuery() {
		logger.debug("Enter doQuery mehtod.");
		//清空查詢結果
		this.preDesignatedAccount = null;
		//檢測用戶輸入的數據
		if(!StringUtils.isNotEmpty(this.securitiesNo)){
			this.errorMessage = "請輸入券商代號！";
			return new ForwardResolution(DEFAULT_JSP);
		}else{
			try {
				//獲取當前登錄用戶所屬群組.
				Long groupPk = UserUtil.getUserGroup().getGroupPk();
				//是否為銀行端
				boolean isBankGroup = UserUtil.isBankGroup();
				//分公司資料
				Securities securities = new Securities();
				//獲取用戶輸入的分公司資料
				if(isBankGroup){
					securities = securitiesService.findForSecuritiesBySecuritiesNo(this.securitiesNo);
				}else{
					securities = securitiesService.findForSecuritiesBySecuritiesNoAndGroupPk(this.securitiesNo, groupPk);
				}
				//如果分公司查無資料
				if(null == securities){
					this.errorMessage = "券商代號有誤！";
					return new ForwardResolution(DEFAULT_JSP);
				}
				//顯示分公司名稱
				this.securitiesName = securities.getSecuritiesName();
				this.groupPk = securities.getGroup().getGroupPk();
				this.securitiesPk = securities.getSecuritiesPk();
				//查詢分公司的券商約定帳號
				try {
					//從db中撈取
					this.preDesignatedAccount = new PreDesignatedAccount();
					if(isBankGroup){
						preDesignatedAccount = preDesignatedAccountService.getPreDesignatedAccountBySecuritiesNo(this.securitiesNo, null);						
					}else{
						preDesignatedAccount = preDesignatedAccountService.getPreDesignatedAccountBySecuritiesNoAndGroupPk(this.securitiesNo, groupPk, null);
					}
					//如果券商約定帳號查無資料
					if(null == preDesignatedAccount){
						//如果資料庫中暫無該分公司的資料,則初始化
						this.preDesignatedAccount = new PreDesignatedAccount();
						this.isNewPreDesignatedAccount = "1";
					}else{
						//啟用Radio Button的選擇
						//不用啟用 TODO
						//取得資料庫中約定帳號的PK
						this.preDesignatedAccountPk = preDesignatedAccount.getPre_Designated_Account_Pk();
					}					 
				} catch(EmptyResultDataAccessException e){
					//如果資料庫中暫無該分公司的資料,則初始化
					this.preDesignatedAccount = new PreDesignatedAccount();
					this.isNewPreDesignatedAccount = "1";
				} catch (Exception e) {
					this.message = "查詢約定帳號資料時發生錯誤,請聯繫系統管理者";
				}
			} catch(EmptyResultDataAccessException e){
				this.message = "分公司查無資料";
			} catch (Exception e) {
				this.message = "查詢分公司資料時發生錯誤,請聯繫系統管理者";
			}
		}
		return new ForwardResolution(DEFAULT_JSP);
	}

	/**
     * 新增/更新
     * @return
     */
    public Resolution doUpdate(){
    	logger.debug("Enter doUpdate mehtod.");
    	//檢查頁面傳值,是否會傳進來NULL
    	if(null == preDesignatedAccount){
    		this.message = "頁面中未鍵入任何資料,請鍵入資料后再進行確認操作.";
    		return new ForwardResolution(DEFAULT_JSP);
    	}
    	User user = UserUtil.getUser();   
    	//Radio Button的設置以及檢查相關資料
    	//必填資料格式檢查標記
    	boolean checkFormat = true;
    	//選填資料格式檢查標記
    	boolean checkFormat2 = true;
    	//欄位長度驗證
    	int checkAccountNoLength = 14;
    	//當第一組被選中時
    	if(StringUtils.isNotEmpty(preDesignatedAccount.getBankBracnchNo1())){
    		//獲取資料,增加部份效能
    		String account_No_H1 = preDesignatedAccount.getAccount_No_H1();
    		String account_No_P1 = preDesignatedAccount.getAccount_No_P1();
    		String account_No_R1 = preDesignatedAccount.getAccount_No_R1();
    		String account_No_H2 = preDesignatedAccount.getAccount_No_H2();
    		String account_No_P2 = preDesignatedAccount.getAccount_No_P2();
    		String account_No_R2 = preDesignatedAccount.getAccount_No_R2();    		
    		//H.一般上市櫃1  資料檢查
    		checkFormat = StringUtils.isNotEmpty(account_No_H1) && account_No_H1.length() == checkAccountNoLength;
    		//P.調撥1  資料檢查
    		if(checkFormat)checkFormat = StringUtils.isNotEmpty(account_No_P1) && account_No_P1.length() == checkAccountNoLength;
    		//R.興櫃1  資料檢查
    		if(checkFormat)checkFormat = StringUtils.isNotEmpty(account_No_R1) && account_No_R1.length() == checkAccountNoLength;
    		//選填資料的驗證
    		if(checkFormat){
    			//如果選填資料不全為空,則進入判斷
    			if(StringUtils.isNotEmpty(account_No_H2) || StringUtils.isNotEmpty(account_No_P2) || StringUtils.isNotEmpty(account_No_R2)){
    				//H.一般上市櫃2  資料檢查
    	    		checkFormat2 = StringUtils.isNotEmpty(account_No_H2) && account_No_H2.length() == checkAccountNoLength;
    	    		//P.調撥2  資料檢查
    	    		if(checkFormat2)checkFormat2 = StringUtils.isNotEmpty(account_No_P2) && account_No_P2.length() == checkAccountNoLength;
    	    		//R.興櫃2  資料檢查
    	    		if(checkFormat2)checkFormat2 = StringUtils.isNotEmpty(account_No_R2) && account_No_R2.length() == checkAccountNoLength;
    			}
    		}
    	}
    	//當設定第二組時
    	else if(StringUtils.isNotEmpty(preDesignatedAccount.getBankBracnchNo2())){

    		//獲取資料,增加部份效能
    		String account_No_H1 = preDesignatedAccount.getAccount_No_H1();
    		String account_No_P1 = preDesignatedAccount.getAccount_No_P1();
    		String account_No_R1 = preDesignatedAccount.getAccount_No_R1();
    		String account_No_H2 = preDesignatedAccount.getAccount_No_H2();
    		String account_No_P2 = preDesignatedAccount.getAccount_No_P2();
    		String account_No_R2 = preDesignatedAccount.getAccount_No_R2();
    		//H.一般上市櫃2  資料檢查
    		checkFormat = StringUtils.isNotEmpty(account_No_H2) && account_No_H2.length() == checkAccountNoLength;
    		//P.調撥2  資料檢查
    		if(checkFormat)checkFormat = StringUtils.isNotEmpty(account_No_P2) && account_No_P2.length() == checkAccountNoLength;
    		//R.興櫃2  資料檢查
    		if(checkFormat)checkFormat = StringUtils.isNotEmpty(account_No_R2) && account_No_R2.length() == checkAccountNoLength;
    		//選填資料的驗證
    		if(checkFormat){
    			//如果選填資料不全為空,則進入判斷
    			if(StringUtils.isNotEmpty(account_No_H1) || StringUtils.isNotEmpty(account_No_P1) || StringUtils.isNotEmpty(account_No_R1)){
    				//H.一般上市櫃2  資料檢查
    	    		checkFormat2 = StringUtils.isNotEmpty(account_No_H1) && account_No_H1.length() == checkAccountNoLength;
    	    		//P.調撥2  資料檢查
    	    		if(checkFormat2)checkFormat2 = StringUtils.isNotEmpty(account_No_P1) && account_No_P1.length() == checkAccountNoLength;
    	    		//R.興櫃2  資料檢查
    	    		if(checkFormat2)checkFormat2 = StringUtils.isNotEmpty(account_No_R1) && account_No_R1.length() == checkAccountNoLength;
    			}
    		}
    	}
    	//如果沒有一組被選中
    	else{
    		this.message = "請設定至少一組券商約定帳號資料再進行儲存.";
    		return new ForwardResolution(DEFAULT_JSP);
    	}
    	//資料格式檢驗失敗,終止更新
    	if(!checkFormat){
    		this.message = "請完整填寫啟用中的券商約定帳號資料再進行儲存.";
    		return new ForwardResolution(DEFAULT_JSP);
    	}
    	//必填資料驗證成功,驗證選填資料
    	else{
    		if(!checkFormat2){
    			this.message = "";
    			return new ForwardResolution(DEFAULT_JSP);
    		}
    	}
    	//群組設定
    	preDesignatedAccount.setGroup_Pk(this.groupPk);
    	//分公司設定
    	preDesignatedAccount.setSecurities_Pk(this.securitiesPk);
    	//是否被刪除
    	preDesignatedAccount.setIs_Deleted(0);
    	//操作人和操作日期的記錄
    	preDesignatedAccount.setCreatedBy(user.getGroup().getGroupNo()+"_"+user.getUserId());
    	preDesignatedAccount.setCreatedDate(new Date());
    	preDesignatedAccount.setUpdatedBy(user.getGroup().getGroupNo()+"_"+user.getUserId());
    	preDesignatedAccount.setUpdatedDate(new Date());
    	//標記 新增或者更新的結果
    	boolean flag;
    	//新增券商約定帳號資料
    	if("1".equals(this.isNewPreDesignatedAccount)){    		
    		int insertResult = preDesignatedAccountService.insert(this.preDesignatedAccount);
    		flag = insertResult > 0 ;
    		this.message = flag?"新增成功！":"新增失敗!";
    	}
    	//更新券商約定帳號資料
    	else{
    		//更新數據
    		if(null != this.preDesignatedAccountPk && 0L != this.preDesignatedAccountPk){
    			int updateResult = preDesignatedAccountService.update(this.preDesignatedAccountPk, this.preDesignatedAccount);
        		flag = updateResult > 0;
        		this.message = flag?"更新成功！":"更新失敗!";
    		}
    		//如果獲取舊約定帳號資料失敗,則終止更新
    		else{
    			flag = false;
    			this.message = "取得券商約定帳號資料失敗,更新終止.";
    		}    		
    	}
    	//如果更新成功,則初始化數據
    	if(flag){
    		this.preDesignatedAccount = null;
    		this.isNewPreDesignatedAccount = "0";
    		this.securitiesName = null;
    		this.securitiesPk = null;
    		this.groupPk = null;
    		this.is_Enabled_Type = null;
    	}
    	return new ForwardResolution(DEFAULT_JSP);
    }
	
	/**
	 * @return account_No_H1
	 */
	public String getAccount_No_H1() {
		return account_No_H1;
	}

	/**
	 * @param account_No_H1 的設定的 account_No_H1
	 */
	public void setAccount_No_H1(String account_No_H1) {
		this.account_No_H1 = account_No_H1;
	}

	/**
	 * @return account_No_H2
	 */
	public String getAccount_No_H2() {
		return account_No_H2;
	}

	/**
	 * @param account_No_H2 的設定的 account_No_H2
	 */
	public void setAccount_No_H2(String account_No_H2) {
		this.account_No_H2 = account_No_H2;
	}

	/**
	 * @return account_No_P1
	 */
	public String getAccount_No_P1() {
		return account_No_P1;
	}

	/**
	 * @param account_No_P1 的設定的 account_No_P1
	 */
	public void setAccount_No_P1(String account_No_P1) {
		this.account_No_P1 = account_No_P1;
	}

	/**
	 * @return account_No_P2
	 */
	public String getAccount_No_P2() {
		return account_No_P2;
	}

	/**
	 * @param account_No_P2 的設定的 account_No_P2
	 */
	public void setAccount_No_P2(String account_No_P2) {
		this.account_No_P2 = account_No_P2;
	}

	/**
	 * @return account_No_R1
	 */
	public String getAccount_No_R1() {
		return account_No_R1;
	}

	/**
	 * @param account_No_R1 的設定的 account_No_R1
	 */
	public void setAccount_No_R1(String account_No_R1) {
		this.account_No_R1 = account_No_R1;
	}

	/**
	 * @return account_No_R2
	 */
	public String getAccount_No_R2() {
		return account_No_R2;
	}

	/**
	 * @param account_No_R2 的設定的 account_No_R2
	 */
	public void setAccount_No_R2(String account_No_R2) {
		this.account_No_R2 = account_No_R2;
	}

	/**
	 * @return is_Enabled_Type
	 */
	public String getIs_Enabled_Type() {
		return is_Enabled_Type;
	}

	/**
	 * @param is_Enabled_Type 的設定的 is_Enabled_Type
	 */
	public void setIs_Enabled_Type(String is_Enabled_Type) {
		this.is_Enabled_Type = is_Enabled_Type;
	}

	/**
	 * @return securitiesName
	 */
	public String getSecuritiesName() {
		return securitiesName;
	}

	/**
	 * @param securitiesName 的設定的 securitiesName
	 */
	public void setSecuritiesName(String securitiesName) {
		this.securitiesName = securitiesName;
	}

	/**
	 * @return securitiesNo
	 */
	public String getSecuritiesNo() {
		return securitiesNo;
	}

	/**
	 * @param securitiesNo 的設定的 securitiesNo
	 */
	public void setSecuritiesNo(String securitiesNo) {
		this.securitiesNo = securitiesNo;
	}

	/**
	 * @return preDesignatedAccount
	 */
	public PreDesignatedAccount getPreDesignatedAccount() {
		return preDesignatedAccount;
	}

	/**
	 * @param preDesignatedAccount 的設定的 preDesignatedAccount
	 */
	public void setPreDesignatedAccount(PreDesignatedAccount preDesignatedAccount) {
		this.preDesignatedAccount = preDesignatedAccount;
	}

	/**
	 * @return isNewPreDesignatedAccount
	 */
	public String getIsNewPreDesignatedAccount() {
		return isNewPreDesignatedAccount;
	}

	/**
	 * @param isNewPreDesignatedAccount 的設定的 isNewPreDesignatedAccount
	 */
	public void setIsNewPreDesignatedAccount(String isNewPreDesignatedAccount) {
		this.isNewPreDesignatedAccount = isNewPreDesignatedAccount;
	}

	/**
	 * @return groupPk
	 */
	public Long getGroupPk() {
		return groupPk;
	}

	/**
	 * @param groupPk 的設定的 groupPk
	 */
	public void setGroupPk(Long groupPk) {
		this.groupPk = groupPk;
	}

	/**
	 * @return securitiesPk
	 */
	public Long getSecuritiesPk() {
		return securitiesPk;
	}

	/**
	 * @param securitiesPk 的設定的 securitiesPk
	 */
	public void setSecuritiesPk(Long securitiesPk) {
		this.securitiesPk = securitiesPk;
	}

	/**
	 * @return preDesignatedAccountPk
	 */
	public Long getPreDesignatedAccountPk() {
		return preDesignatedAccountPk;
	}

	/**
	 * @param preDesignatedAccountPk 的設定的 preDesignatedAccountPk
	 */
	public void setPreDesignatedAccountPk(Long preDesignatedAccountPk) {
		this.preDesignatedAccountPk = preDesignatedAccountPk;
	}

}
